package com.hongdee.crm.average.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.crm.average.dao.IAverageDao;
import com.hongdee.crm.average.entity.TCrmAverageEntity;
import com.hongdee.crm.average.service.ITCrmIAverageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
/**
 * Created by dell on 2017/5/26.
 */
public class TCrmIAverageServiceimpl  implements ITCrmIAverageService {
    @Autowired
    private IAverageDao iAverageDao;
    private String tabName = "T_CRM_AVERAGE";
    @Autowired
    private IHibernateDao iHibernateDao;

    /**
     * @param tCrmAverageEntity 实体
     * @return tCrmAverageEntity
     * @throws Exception
     * @paramuid 登录用户 ID
     * @desc 新增与更改 保存数据
     * @author 于扬
     */

    public TCrmAverageEntity saveOrUpdate(TCrmAverageEntity tCrmAverageEntity) throws Exception {
        int carNum = Integer.valueOf(tCrmAverageEntity.getCarNum());
        int acz = Integer.valueOf(tCrmAverageEntity.getAverageCz());
        int wcz = Integer.valueOf(tCrmAverageEntity.getWorkorderNum());
        tCrmAverageEntity.setYearCz(String.valueOf(carNum * acz * wcz));
        //判断id存在不存在如果为null则是新增


        if (null == tCrmAverageEntity.getId() || StringUtils.isEmpty(tCrmAverageEntity.getId())) {
            this.iAverageDao.saveOrUpdate(tCrmAverageEntity);
        } else {
            //如果有id则是更改
            int carNum1 = Integer.valueOf(tCrmAverageEntity.getCarNum());
            int acz1 = Integer.valueOf(tCrmAverageEntity.getAverageCz());
            int wcz1 = Integer.valueOf(tCrmAverageEntity.getWorkorderNum());
            tCrmAverageEntity.setYearCz(String.valueOf(carNum1 * acz1 * wcz1));
            this.iAverageDao.saveOrUpdate(tCrmAverageEntity);
        }
        //返回实体
        return tCrmAverageEntity;
    }

    /**
     * @param id 用户 ID
     * @return tCrmAverageEntity
     * @throws Exception
     * @desc 按条件查询
     * @author 于扬
     */

    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.iAverageDao.info(sql, id);
    }

    /**
     * @param start
     * @param limit
     * @return tCrmAverageEntity
     * @throws Exception
     * @paramuid 登录用户 ID
     * @desc 分页查询
     * @author 于扬
     */

    public Map newPage(String start, String limit, String year) throws Exception {

        String sql = "select * from  T_CRM_AVERAGE where 1=1 ";
        List<Object> params = new ArrayList();
        if (!StringUtils.isEmpty(year)) {
            sql += "and year= ?";
            params.add(year);
        }
        return this.iAverageDao.page(sql, params.toArray(), start, limit);
    }
    public Map save(String baseTime) throws Exception{
        String sql="INSERT INTO T_CRM_AVERAGE (id, base_time, base_car, forecast_year, car_type, car_age, consumption_type, car_num, YEAR ) SELECT id, base_time, base_car, forecast_year, car_type, car_age, consumption_type, car_num, YEAR FROM t_crm_average_car WHERE base_time = ?";
        this.iAverageDao.executeSQL(sql,baseTime);
        return null;
    }
}